<template>
  <div class="default-main ba-table-box">
    <!-- 表格顶部菜单 -->
    <TableHeader
      :buttons="['refresh', 'remove', 'edit', 'columnDisplay']"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
    <Table ref="tableRef" @action="baTable.onTableAction" />
  </div>
</template>
<script lang="ts" setup>
import {
  ref,
  provide,
  onMounted,
  markRaw,
  reactive,
  onBeforeUnmount,
} from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import { ProductionWorkReport } from "@/api/backend/index";
import { useRoute, useRouter } from "vue-router";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useNavTabs } from "@/store/navTabs";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const router = useRouter();
const navTabs = useNavTabs();

//全局事件总线
const { proxy } = useCurrentInstance();
const state: {} = reactive({});

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + ProductionWorkReport)
      ? (navTabs as any).pageSizeMap.get(route.path + ProductionWorkReport)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(ProductionWorkReport), //this.api = api部门管理
  {
    expandAll: true,
    pk: "id",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("workOrderId"),
        label: "工单ID",
        prop: "workOrderId",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("planCode"),
        label: "批次号",
        prop: "planCode",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("processName"),
        label: "工序名称",
        prop: "processName",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("materialType"),
        label: "产品类型",
        prop: "materialType",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("equipmentCode"),
        label: "设备编码",
        prop: "equipmentCode",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("personName"),
        label: "操作人员",
        prop: "personName",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("planQuantity"),
        label: "计划数",
        prop: "planQuantity",
        align: "left",
        operator: false,
        // render: "customTemplate",
        // customTemplate: (row: TableRow, field: TableColumn, value: any) => {
        //   return (
        //     "<p>" +
        //     row.planQuantity * (row.perUsage ? row.perUsage : 1) +
        //     "</p>"
        //   );
        // },
      },
      {
        show: filedShowOff("completeQuantity"),
        label: "完成数量",
        prop: "completeQuantity",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("selfCheckQuantity"),
        label: "自检数量",
        prop: "selfCheckQuantity",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("createTime"),
        label: "实际开始时间",
        prop: "createTime",
        align: "center",
        render: "datetime",
      },
      {
        show: filedShowOff("updateTime"),
        label: "实际结束时间",
        prop: "updateTime",
        align: "center",
        render: "datetime",
      },
      {
        show: operatorShowOff([
          route.path + "/edit",
          route.path + "/add",
          route.path + "/remove",
        ]),
        label: "操作",
        align: "center",
        operator: false,
        render: "buttons",
        fixed: "right",
        width: "150",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      pageSize: pageSizeKeep(),
    },
    extend: {
      maxHeight: 580,
    },
  },
  {},
  {}
);

onMounted(() => {
  //全局事件总线
  const { proxy } = useCurrentInstance();
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(ProductionWorkReport);
  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */

  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {}
  );
});
// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
</script>
<style lang="scss" scoped></style>
